import re
import os.path
import urllib2, urllib

COOKIEFILE = 'cookies.lwp'
# the path and filename to save your cookies in

cj = None
ClientCookie = None
cookielib = None

# Let's see if cookielib is available
try:
    import cookielib
except ImportError:
    # If importing cookielib fails
    # let's try ClientCookie
    try:
        import ClientCookie
    except ImportError:
        # ClientCookie isn't available either
        urlopen = urllib2.urlopen
        Request = urllib2.Request
    else:
        # imported ClientCookie
        urlopen = ClientCookie.urlopen
        Request = ClientCookie.Request
        cj = ClientCookie.LWPCookieJar()

else:
    # importing cookielib worked
    urlopen = urllib2.urlopen
    Request = urllib2.Request
    cj = cookielib.LWPCookieJar()
    # This is a subclass of FileCookieJar
    # that has useful load and save methods
    
if cj is not None:
# we successfully imported
# one of the two cookie handling modules

    if os.path.isfile(COOKIEFILE):
        # if we have a cookie file already saved
        # then load the cookies into the Cookie Jar
        cj.load(COOKIEFILE)

    # Now we need to get our Cookie Jar
    # installed in the opener;
    # for fetching URLs
    if cookielib is not None:
        # if we use cookielib
        # then we get the HTTPCookieProcessor
        # and install the opener in urllib2
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        urllib2.install_opener(opener)

    else:
        # if we use ClientCookie
        # then we get the HTTPCookieProcessor
        # and install the opener in ClientCookie
        opener = ClientCookie.build_opener(ClientCookie.HTTPCookieProcessor(cj))
        ClientCookie.install_opener(opener)


req = Request('http://zig.weiyu.org/blog/archives/294')
f = urlopen(req)
print f.info()
if cj is None:
    print "We don't have a cookie library available - sorry."
    print "I can't show you any cookies."
else:
    print 'These are the cookies we have received so far :'
    for index, cookie in enumerate(cj):
        print index, '  :  ', cookie
    cj.save(COOKIEFILE)                     # save the cookies again

for line in f.readlines():
    line = line.strip()
    pat = re.findall('<input type="hidden" name="mcspinfo" value="(.*)" />',line)
    if pat:
        mcspinfo = pat[0]
    pat = re.findall('<label for="mcspvalue"><small>Spam protection: Sum of (.*) \+ (.*) \?</small></label>',line)
    if pat:
        mcspvalue = int(pat[0][0])+int(pat[0][1])
import time
param = urllib.urlencode({'url':'',
                          'author':'rrewqt1',
                          'email':'have@fun.oo',
                          'mcspvalue':mcspvalue,
                          'mcspinfo':mcspinfo,
                          'comment_post_ID':294,
                          'submit':'Submit Comment',
                          'comment':time.time(),
                          })
print 'http://zig.weiyu.org/blog/wp-comments-post.php?'+param

txheaders =  {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
              'Referer':'http://zig.weiyu.org/blog/archives/294',
              'Connection':'keep-alive',
              'Keep-Alive':300}

dl = Request('http://zig.weiyu.org/blog/wp-comments-post.php', param, txheaders)
print urlopen
reslt = urlopen(dl)
print reslt.info()
print reslt.read()

if cj is None:
    print "We don't have a cookie library available - sorry."
    print "I can't show you any cookies."
else:
    print 'These are the cookies we have received so far :'
    for index, cookie in enumerate(cj):
        print index, '  :  ', cookie
    cj.save(COOKIEFILE)                     # save the cookies again
